package com.promis.widget.mediapicker.ui;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.promis.util.StringUtils;
import com.promis.widget.R;
import com.promis.widget.mediapicker.MediaPickerConfig;
import com.promis.widget.mediapicker.entity.ImageParcelable;
import com.promis.widget.photoview.OnPhotoTapListener;
import com.promis.widget.photoview.PhotoView;
import com.promis.widget.photoview.PhotoViewAttacher;


/**
 * Created by dmcBig on 2017/8/16.
 */

public class ImagePreviewFragment extends Fragment{
    private PhotoView mPhotoView;
    ImageView play_view;
    private PhotoViewAttacher mAttacher;

    public static ImagePreviewFragment newInstance(ImageParcelable image, boolean close) {
        ImagePreviewFragment f = new ImagePreviewFragment();
        Bundle b = new Bundle();
        b.putParcelable("image",image);
        b.putBoolean("close",close);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mediapicker_item, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);
        ImageParcelable image=getArguments().getParcelable("image");

        play_view=(ImageView) view.findViewById(R.id.play_view);
        mPhotoView = (PhotoView) view.findViewById(R.id.photoview);
        mAttacher = new PhotoViewAttacher(mPhotoView);
//        mAttacher.setRotatable(true);
//        mAttacher.setToRightAngle(true);

//        setPlayView(image);

        Uri mediaUri = null;
        if (!StringUtils.isNullOrEmpty(image.localPath)) {
            mediaUri = Uri.parse("file://" + image.localPath);
        } else if (!StringUtils.isNullOrEmpty(image.httpOriginalPath)) {
            mediaUri = Uri.parse(MediaPickerConfig.httpUrl + image.httpOriginalPath);
        } else if (!StringUtils.isNullOrEmpty(image.compressPath)) {
            mediaUri = Uri.parse("file://" + image.compressPath);
        }
        Glide.with(getContext()).load(mediaUri).into(mPhotoView);

        boolean close = getArguments().getBoolean("close");
        if (close) {
            mAttacher.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(ImageView view, float x, float y) {
                    getActivity().finish();
                }
            });
        }
    }

//    void setPlayView(final MediaParcelable media){
//        if(media.mediaType==3){
//            play_view.setVisibility(View.VISIBLE);
//            play_view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Uri uri = Uri.parse(media.path);
//                    Intent intent = new Intent(Intent.ACTION_VIEW);
//                    intent.setDataAndType(uri, "video/*");
//                    startActivity(intent);
//                }
//            });
//        }
//    }

    @Override
    public void onDestroyView() {
//        mAttacher.cleanup();
        super.onDestroyView();
    }
}
